package com.xxl.job.consume.repository;

import com.xxl.job.consume.common.RedisKeyConstant;
import com.xxl.job.consume.model.SignalDefHkModel;
import com.xxl.job.consume.utils.JSONUtils;
import com.xxl.job.consume.utils.MapUtil;
import com.xxl.job.core.log.XxlJobLogger;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 查询信号
 */
@Repository
public class SignalDefRepository {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 查询环控信号
     * @param gatherId
     * @param gatherResId
     * @param pointAddr
     * @return
     */
    public SignalDefHkModel queryHkSignal(String gatherId, String gatherResId, String pointAddr) {
        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.KEY_HASH_POW_HK_SIGNAL);

        String value=valueOperations.get("HK_"+gatherId+"_"+gatherResId+"_"+pointAddr);

        if (StringUtils.isNotBlank(value)){
            try {
                SignalDefHkModel signalDefHkModel = JSONUtils.json2pojo(value, SignalDefHkModel.class);
                return signalDefHkModel;
            } catch (IOException e) {
                XxlJobLogger.log("查询信号：,终端JSON转换异常！", value, e);
            }
        }
        XxlJobLogger.log("没有{}对应的信号配置！", gatherId+gatherResId+pointAddr);
        return null;
    }



    /**
     * 查询环控信号 多个
     * @param gatherId
     * @return
     */
    public List<SignalDefHkModel> queryHkSignalList(String gatherId, String gatherResId, String pointAddr){

        List<SignalDefHkModel> signalDefHkModellList = new ArrayList<>();
        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.KEY_HASH_POW_HK_SIGNAL);
        List<String> signalDefHkModelJsonList = MapUtil.getLikeByMap(valueOperations.entries(),"HK_"+gatherId+"_"+gatherResId+"_"+pointAddr);
        if (signalDefHkModelJsonList != null && signalDefHkModelJsonList.size() > 0){
            for (String signalDefHkModelStr : signalDefHkModelJsonList) {
                if (StringUtils.isNotBlank(signalDefHkModelStr)) {
                    try {
                        SignalDefHkModel gdm = JSONUtils.json2pojo(signalDefHkModelStr, SignalDefHkModel.class);
                        if(gatherId.equals(gdm.getGatherId())){
                            signalDefHkModellList.add(gdm);
                        }
                    } catch (IOException e) {
                        XxlJobLogger.log("获取采集口配置信息终端JSON='{}' 转换异常！", signalDefHkModelStr, e);
                    }
                }
            }
            return signalDefHkModellList;
        }else {
            XxlJobLogger.log("没有'{}' 对应的信号配置缓存！", gatherId);
            return null;
        }
    }
//    /**
//     * 查询入侵信号
//     * @param gatherId
//     * @param gatherResId
//     * @param pointAddr
//     * @return
//     */
//    public SignalDefRqModel queryRqSignal(String gatherId, String gatherResId, String pointAddr) {
//        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.KEY_HASH_POW_RQ_SIGNAL);
//
//        String value=valueOperations.get("RQ_"+gatherId+"_"+gatherResId+"_"+pointAddr);
//
//        if (StringUtils.isNotBlank(value)){
//            try {
//                SignalDefRqModel signalDefRqModel = JSONUtils.json2pojo(value, SignalDefRqModel.class);
//                return signalDefRqModel;
//            } catch (IOException e) {
//                XxlJobLogger.log("查询信号：,终端JSON转换异常！", value, e);
//            }
//        }
//        XxlJobLogger.log("没有{}对应的信号配置！", gatherId+gatherResId+pointAddr);
//        return null;
//    }
//
//    /**
//     * 查询电话信号
//     * @param gatherId
//     * @param gatherResId
//     * @param pointAddr
//     * @return
//     */
//    public SignalDefDhModel queryDhSignal(String gatherId, String gatherResId, String pointAddr) {
//        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.KEY_HASH_POW_DH_SIGNAL);
//
//        String value=valueOperations.get("DH_"+gatherId+"_"+gatherResId+"_"+pointAddr);
//
//        if (StringUtils.isNotBlank(value)){
//            try {
//                SignalDefDhModel signalDefDhModel = JSONUtils.json2pojo(value, SignalDefDhModel.class);
//                return signalDefDhModel;
//            } catch (IOException e) {
//                XxlJobLogger.log("查询信号：,终端JSON转换异常！", value, e);
//            }
//        }
//        XxlJobLogger.log("没有{}对应的信号配置！", gatherId+gatherResId+pointAddr);
//        return null;
//    }
//
//    /**
//     * 查询消防信号
//     * @param gatherId
//     * @param gatherResId
//     * @param pointAddr
//     * @return
//     */
//    public SignalDefXfModel queryXfSignal(String gatherId, String gatherResId,String registerAddr,String moduleAddr, String pointAddr) {
//        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.KEY_HASH_POW_XF_SIGNAL);
//
//        String value=valueOperations.get("XF_"+gatherId+"_"+gatherResId+"_"+registerAddr+
//                "_"+moduleAddr+"_"+pointAddr);
//
//        if (StringUtils.isNotBlank(value)){
//            try {
//                SignalDefXfModel signalDefXfModel = JSONUtils.json2pojo(value, SignalDefXfModel.class);
//                return signalDefXfModel;
//            } catch (IOException e) {
//                XxlJobLogger.log("查询信号：,终端JSON转换异常！", value, e);
//            }
//        }
//        XxlJobLogger.log("没有{}对应的信号配置！", gatherId+gatherResId+pointAddr);
//        return null;
//    }
//
//    /**
//     * 查询AP信号
//     * @param gatherId
//     * @param gatherResId
//     * @param pointAddr
//     * @return
//     */
//    public SignalDefApModel queryApSignal(String gatherId, String gatherResId, String pointAddr) {
//        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.KEY_HASH_POW_AP_SIGNAL);
//
//        String value=valueOperations.get("GATHER_"+gatherId+"_"+gatherResId+"_"+pointAddr);
//
//        if (StringUtils.isNotBlank(value)){
//            try {
//                SignalDefApModel signalDefApModel = JSONUtils.json2pojo(value, SignalDefApModel.class);
//                return signalDefApModel;
//            } catch (IOException e) {
//                XxlJobLogger.log("查询信号：,终端JSON转换异常！", value, e);
//            }
//        }
//        XxlJobLogger.log("没有{}对应的信号配置！", gatherId+gatherResId+pointAddr);
//        return null;
//    }
//
//    /**
//     * 查询采集口信号
//     * @param gatherId
//     * @param gatherResId
//     * @param pointAddr
//     * @return
//     */
//    public SignalDefGatherModel queryGatherSignal(String gatherId, String gatherResId, String pointAddr) {
//        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.KEY_HASH_POW_GATHER_SIGNAL);
//
//        String value=valueOperations.get("GATHER_"+gatherId+"_"+gatherResId+"_"+pointAddr);
//
//        if (StringUtils.isNotBlank(value)){
//            try {
//                SignalDefGatherModel signalDefGatherModel = JSONUtils.json2pojo(value, SignalDefGatherModel.class);
//                return signalDefGatherModel;
//            } catch (IOException e) {
//                XxlJobLogger.log("查询信号：,终端JSON转换异常！", value, e);
//            }
//        }
//        XxlJobLogger.log("没有{}对应的信号配置！", gatherId+gatherResId+pointAddr);
//        return null;
//    }

}
